package cn.ym.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.ym.domain.FriendAddress;
import cn.ym.exception.BusinessException;
import cn.ym.mapper.FriendAddressMapper;
import cn.ym.query.FriendAddressQuery;
import cn.ym.service.FriendAddressService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 链接的业务层
 */
@Service
public class FriendAddressServiceImpl implements FriendAddressService {

    @Autowired
    private FriendAddressMapper friendAddressMapper;

    @Override
    public void add(FriendAddress friendAddress) {
        //空值检验
        if(StrUtil.isEmptyIfStr(friendAddress.getName())){
            throw new BusinessException("亲：链接名称不能为空");
        }
        friendAddressMapper.add(friendAddress);
    }

    @Override
    public void update(FriendAddress friendAddress) {
        //空值检验
        if(StrUtil.isEmptyIfStr(friendAddress.getName())){
            throw new BusinessException("亲：链接名称不能为空");
        }
        friendAddressMapper.update(friendAddress);
    }

    @Override
    public void delete(Long id) {

        friendAddressMapper.delete(id);
    }

    @Override
    public void Patchdel(Long[] ids) {

        friendAddressMapper.Patchdel(ids);
    }

    @Override
    public FriendAddress findOne(Long id) {

        return friendAddressMapper.findOne(id);
    }

    @Override
    public List<FriendAddress> findAll() {

        return friendAddressMapper.findAll();
    }


    @Override
    public List<FriendAddress> queryPage(FriendAddressQuery friendAddressQuery) {
        //开启分页
        /**1.自动计算总数量
         * 2.根据驱动名计算 limit的第一个参数
         * 3.分页返回结果
         */
       PageHelper.startPage(friendAddressQuery.getCurrentPage(), friendAddressQuery.getPageSize());
       return friendAddressMapper.queryPage(friendAddressQuery);
    }
}